using System.Collections.Generic;
using I2.Loc;
using UnityEngine;

public class TutorialStepKill : TutorialBase
{
	protected enum TutorialStepKillPhase
	{
		SpawnMob = 0,
		WalkMob = 1,
		KillMob = 2
	}

	public List<MobParameters> listMobs;

	public List<MobPoint> listMobSpawnPoints;

	public List<MobPoint> listMobMovePoints;

	public float aggressiveDistance = 2f;

	public string message;

	public string attackMessage;

	protected List<GameObject> arrowMobs = new List<GameObject>();

	protected bool chasingStart;

	protected List<EnemyBehavior> enemyList = new List<EnemyBehavior>();

	protected PlayerBehavior player;

	protected TutorialStepKillPhase currenState;

	protected List<bool> mobComePoint = new List<bool>();

	private TutorialButton btnSwitchWeapon;

	public bool switchWeaponComplete;

	private SlotSelector slotSelector;

	public override void OnStart()
	{
		base.OnStart();
		player = GameController.instance.playerScript;
		SpawnMob();
		TutorialManager.instance.stepTutorialForFlurry = "7 kill gun";
		btnSwitchWeapon = TutorialManager.instance.GetTutorialButtonById(TutorialButtonId.switchWeapon);
		GameController.instance.playerScript.weaponManager.switchWeapon("rifles", "Tutorial_Famas", string.Empty);
		btnSwitchWeapon.SetActive(true);
		slotSelector = WalkWindow.instance.slotSelector;
		TutorialManager.instance.PlayAnimButton(TutorialButtonId.switchWeapon);
	}

	protected void SpawnMob()
	{
		Debug.LogError("SpawnMob");
		currenState = TutorialStepKillPhase.SpawnMob;
		for (int i = 0; i < listMobs.Count; i++)
		{
			EnemyBehavior enemyBehavior = MobSpawnController.instance.SingleMobSpawn(listMobs[i], listMobSpawnPoints[i], null, false);
			enemyBehavior.SwitchEnemyBehaviourModel(BehaviorModel.Passive);
			PointerController.instance.AddPointer(enemyBehavior.transform, 0f, "iconMap_Red Cercle", false);
			enemyList.Add(enemyBehavior);
			mobComePoint.Add(false);
		}
		ShowText(ScriptLocalization.Get("Tutorial/" + message), 100000f, true);
	}

	protected void StartMoveMob()
	{
		currenState = TutorialStepKillPhase.WalkMob;
		for (int i = 0; i < enemyList.Count; i++)
		{
			enemyList[i].mobNavigationController.GoToTarget(listMobMovePoints[i].position);
			arrowMobs.Add(TutorialManager.instance.AddUkazatelOn(enemyList[i].gameObject));
		}
	}

	public override void OnUpdate()
	{
		if (!switchWeaponComplete && GameController.instance.playerScript.weaponManager.currentWeaponScript.name == "Tutorial_Famas")
		{
			switchWeaponComplete = true;
			slotSelector.blocked = true;
			TutorialManager.instance.PlayAnimButton(TutorialButtonId.shoot, TutorialManager.instance.animBtnTap);
			ShowText(ScriptLocalization.Get("Tutorial/" + attackMessage), 100000f);
		}
		switch (currenState)
		{
		case TutorialStepKillPhase.SpawnMob:
			StartMoveMob();
			break;
		case TutorialStepKillPhase.WalkMob:
		{
			if (mobComePoint.TrueForAll((bool b) => b))
			{
				currenState = TutorialStepKillPhase.KillMob;
				enemyList.ForEach(delegate(EnemyBehavior e)
				{
					e.enemyAreaBehavior.aggressiveDistance = aggressiveDistance;
				});
				return;
			}
			for (int i = 0; i < enemyList.Count; i++)
			{
				if (!mobComePoint[i] && Vector3.Distance(enemyList[i].transform.position, listMobMovePoints[i].position) < 0.3f)
				{
					mobComePoint[i] = true;
					enemyList[i].StopMechanim();
					enemyList[i].mobNavigationController.DisableNavigation();
					enemyList[i].mobNavigationController.rotationTarget = GameController.instance.playerScript.transform;
				}
			}
			if (Time.frameCount % 3 == 0)
			{
				for (int j = 0; j < enemyList.Count; j++)
				{
					if (!mobComePoint[j])
					{
					}
				}
			}
			if (!enemyList.TrueForAll((EnemyBehavior e) => e.health == e.maxHealth))
			{
				currenState = TutorialStepKillPhase.KillMob;
			}
			if (enemyList.TrueForAll((EnemyBehavior e) => e.isDead))
			{
				OnComplete();
			}
			break;
		}
		case TutorialStepKillPhase.KillMob:
			if (!chasingStart && !enemyList.TrueForAll((EnemyBehavior e) => e.health == e.maxHealth))
			{
				enemyList.ForEach(delegate(EnemyBehavior e)
				{
					e.GetDamage(0, 1);
					StartChasing(e);
				});
				chasingStart = true;
			}
			if (chasingStart && Time.frameCount % 3 == 0)
			{
				enemyList.ForEach(delegate(EnemyBehavior e)
				{
					if (!e.isDead && Vector3.Distance(e.transform.position, player.transform.position) > e.enemyAreaBehavior.aggressiveDistance * 0.9f)
					{
						if (!e.mobNavigationController.walking)
						{
							StartChasing(e);
						}
					}
					else if (!e.isDead)
					{
						e.mobNavigationController.DisableNavigation();
						e.StopMechanim();
						e.mobNavigationController.DisableNavigation();
					}
				});
			}
			if (enemyList.TrueForAll((EnemyBehavior e) => e.isDead))
			{
				OnComplete();
			}
			break;
		}
		if (player.health != player.maxHealth)
		{
			player.health = player.maxHealth;
		}
		if (player.weaponManager.currentWeaponScript != null)
		{
			player.weaponManager.currentWeaponScript.ammo = player.weaponManager.currentWeaponScript.MaxAmmo;
		}
	}

	public override void OnComplete()
	{
		TutorialManager.instance.tony.SetTarget(GameController.instance.playerScript.transform);
		slotSelector.blocked = true;
		btnSwitchWeapon.SetBlock(true);
		PointerController.instance.RemoveAllPointers();
		arrowMobs.ForEach(delegate(GameObject a)
		{
			Object.Destroy(a);
		});
		base.OnComplete();
	}

	protected void StartChasing(EnemyBehavior behavior)
	{
		behavior.StartMechanim(behavior.mobNavigationController.runSpeed);
		behavior.mobNavigationController.StartMoving(behavior.mobNavigationController.runSpeed);
		behavior.mobNavigationController.GoToTarget(player.transform, behavior.enemyAreaBehavior.aggressiveDistance * 0.8f);
	}

	private void OnDrawGizmosSelected()
	{
		for (int i = 0; i < listMobSpawnPoints.Count; i++)
		{
			Gizmos.color = Color.blue;
			Gizmos.DrawCube(listMobSpawnPoints[i].position + Vector3.up * 0.9f, new Vector3(0.5f, 2f, 0.5f));
			Gizmos.color = Color.red;
			Gizmos.DrawCube(listMobMovePoints[i].position + Vector3.up * 0.9f, new Vector3(0.5f, 2f, 0.5f));
			Gizmos.color = Color.yellow;
			Gizmos.DrawLine(listMobSpawnPoints[i].position + Vector3.up * 1.5f, listMobMovePoints[i].position + Vector3.up * 1.5f);
		}
	}
}
